package chatserver.logging;




import java.text.DateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

public class ServerLogFormatter extends Formatter {

    private DateFormat dateformater;

    public ServerLogFormatter(boolean withDate) {
        super();

        if (withDate) {
            dateformater = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        } else {
            dateformater = DateFormat.getTimeInstance(DateFormat.MEDIUM);
        }
    }

    @Override
    public String format(LogRecord record) {
        Date date = new Date();
        date.setTime(record.getMillis());
        StringBuilder sb = new StringBuilder();
        
        // Date
        sb.append("[");
        sb.append(dateformater.format(date));
        sb.append("] ");


        // Level
        sb.append(record.getLevel().getName().charAt(0));
        sb.append(" ");

        // Message
        sb.append(record.getMessage());

        sb.append(" [");
        // Class
        if (record.getSourceClassName() != null) {
            sb.append(record.getSourceClassName());
        }

        // Method
        if (record.getSourceMethodName() != null) {
            sb.append(".");
            sb.append(record.getSourceMethodName());
            sb.append("()");
        }
        sb.append("] ");
        
        // Newline
        sb.append(System.getProperty("line.separator"));

        return sb.toString();
    }
}
